package com.makeunion.gdlbsdemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.district.DistrictItem;
import com.amap.api.services.district.DistrictResult;
import com.amap.api.services.district.DistrictSearch;
import com.amap.api.services.district.DistrictSearchQuery;
import com.makeunion.gdlbsdemo.R;

import java.util.ArrayList;
import java.util.List;


/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class SearchDistrictActivity extends AppCompatActivity {

    private MapView mapView;

    private AMap aMap;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_district);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                searchDistrict();
            }
        }, 1000);
    }

    private void searchDistrict() {
        DistrictSearch search = new DistrictSearch(this);
        DistrictSearchQuery query = new DistrictSearchQuery();
        query.setKeywords("朝阳区");//传入关键字
        query.setShowBoundary(true);//是否返回边界值
        search.setQuery(query);
        search.setOnDistrictSearchListener(new DistrictSearch.OnDistrictSearchListener() {
            @Override
            public void onDistrictSearched(DistrictResult result) {
                ArrayList<DistrictItem> district = result.getDistrict();
                for (DistrictItem districtItem : district) {
                    showBoundary(districtItem);

//                    String adcode = districtItem.getAdcode();
//                    LatLonPoint center = districtItem.getCenter();
//                    String citycode = districtItem.getCitycode();
//                    String level = districtItem.getLevel();
//                    String name = districtItem.getName();
//                    List<DistrictItem> subDistrict = districtItem.getSubDistrict();
                }
            }
        });
//        search.searchDistrictAnsy();
        search.searchDistrictAsyn();
    }

    private void showBoundary(DistrictItem districtItem) {
        String[] polyStr = districtItem.districtBoundary();
        if (polyStr == null || polyStr.length == 0) {
            return;
        }
        for (String str : polyStr) {
            String[] lat = str.split(";");
            PolylineOptions polylineOption = new PolylineOptions();
            boolean isFirst = true;
            LatLng firstLatLng = null;
            for (String latstr : lat) {
                String[] lats = latstr.split(",");
                if (isFirst) {
                    isFirst = false;
                    firstLatLng = new LatLng(Double
                            .parseDouble(lats[1]), Double
                            .parseDouble(lats[0]));
                }
                polylineOption.add(new LatLng(Double
                        .parseDouble(lats[1]), Double
                        .parseDouble(lats[0])));
            }
            if (firstLatLng != null) {
                polylineOption.add(firstLatLng);
            }

            polylineOption.width(6).color(Color.BLUE);
            aMap.addPolyline(polylineOption);
        }
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
